package cn.tedu.spring.controller;

import cn.tedu.spring.entity.Dept;
import cn.tedu.spring.entity.User;
import cn.tedu.spring.service.DeptService;
import cn.tedu.spring.service.UserService;
import cn.tedu.spring.vo.UserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Author：程Sir
 * Name： framework
 * Content:
 */
@Controller
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserService userService;

    @Resource
    private DeptService deptService;
    //展示所有数据
    @RequestMapping("/findAll.do")
    public String findAll(ModelMap modelMap, HttpServletRequest request){
        List<UserVO> users =  userService.findAll();
        System.out.println(users);
        modelMap.addAttribute("users",users);
        modelMap.addAttribute("context",request.getContextPath());
        return "index";
    }

    //展示登录页面
    @RequestMapping("/toLogin.do")
    public String toLogin(ModelMap modelMap, HttpServletRequest request){
        modelMap.addAttribute("context",request.getContextPath());
        return "login";
    }
    // 登录功能
    @RequestMapping("/login.do")
    public String login(String username, String password, ModelMap modelMap, HttpServletRequest request){
        //1. 查询是否有名为username的用户
        User user = userService.findUserByUsername(username);
        System.out.println("login.."+user);
        //2. 判断
        if (user != null){
            // 2.1 如何用户存在，则判断密码是否是password
            if (user.getPassword().equals(password)){
                // 2.2 如果存在则验证通过，登录成功！
                return "redirect:/user/findAll.do";
            }
            // 一般情况下这个提示不会做的，危险！
            modelMap.addAttribute("message"," 该填写的密码不正确");
            System.out.println("用户输入的密码不对！");

        }
        // 3. 不存在则输出用户不存在页面
        modelMap.addAttribute("message",username+" 该用户不存在或填写的用户名不正确");
        modelMap.addAttribute("context",request.getContextPath());
        return "message";
    }

    //展示登录页面
    @RequestMapping("/toRegister.do")
    public String toRegister(ModelMap modelMap, HttpServletRequest request){
        List<Dept> depts = deptService.findAll();
        System.out.println(depts);
        modelMap.addAttribute("depts",depts);
        modelMap.addAttribute("context",request.getContextPath());
        return "register";
    }

    @RequestMapping("/register.do")
    public String register(User user,ModelMap modelMap, HttpServletRequest request){
        String message = userService.addUser(user);
        if (!"ok".equals(message)){
            modelMap.addAttribute("context",request.getContextPath());
            modelMap.addAttribute("message",message);
            return "message";
        }
        return "redirect:/user/toLogin.do";
    }
    // 删除用户信息
    @RequestMapping("/delete.do")
    public String delete(Integer id,ModelMap modelMap, HttpServletRequest request){
        String message = userService.removeUser(id);
        if (!"ok".equals(message)){
            modelMap.addAttribute("context",request.getContextPath());
            modelMap.addAttribute("message",message);
            return "message";
        }
        return "redirect:/user/findAll.do";
    }

    @RequestMapping("/toUpdate.do")
    public String toUpdate(Integer id,ModelMap modelMap, HttpServletRequest request){
        User user = userService.findUserById(id);
        if (user==null){
            modelMap.addAttribute("context",request.getContextPath());
            modelMap.addAttribute("message","更新时发生未知错误，稍后重试！");
            return "message";
        }
        modelMap.addAttribute("context",request.getContextPath());
        modelMap.addAttribute("user",user);
        return "update";
    }
    // 更新用户数据
    @RequestMapping("/update.do")
    public String update(User user,ModelMap modelMap, HttpServletRequest request){
        System.err.println(user);
        String message = userService.chageUser(user);
        if (!"ok".equals(message)){
            modelMap.addAttribute("context",request.getContextPath());
            modelMap.addAttribute("message",message);
            return "message";
        }
        return "redirect:/user/findAll.do";
    }


}
